package com.boding.power.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;

import com.boding.power.common.Constants;
import com.boding.power.dto.UserDto;
import com.boding.power.intercept.PerfectInfoInterceptor;
import com.boding.power.model.UserRoles;
import com.boding.power.model.Users;
import com.boding.power.utils.CryptographyUtil;
import com.boding.power.utils.DataGrid;
import com.boding.power.utils.UserUtil;
import com.jfinal.aop.Before;
import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.render.CaptchaRender;

//@Before(LoginInterceptor.class)
public class UserController extends Controller {
	/**
	 * render 是按照 /xx 是相对于 webapp xx 是相对于你配置的基路径
	 */
	@ActionKey("/login")
	public void loginUI() {
		render("login.jsp");
	}
	
	//获取个人信息,没有个人信息或者商户信息 跳转到完善个人信息
	@Before(value={PerfectInfoInterceptor.class})
	public void index(){
		render("index.jsp");
	}
	//admin/user/login
	public void login() {
		Users userDto = getModel(Users.class);
		Subject currentUser = SecurityUtils.getSubject();
		boolean remember = getPara("rememberMe") == null ? false : true;
		//boolean loginSuccess = CaptchaRender.validate(this, getPara("code"));
		 boolean loginSuccess = true; //开发先设置为true
		// 校验用户信息
		if (loginSuccess) {
			UsernamePasswordToken token = new UsernamePasswordToken(
					userDto.getUsername(), userDto.getPassword());
			try {
				token.setRememberMe(true);// 是否记住我
				currentUser.login(token);
				redirect("/admin/user/");
			} catch (Exception e) {
				setAttr("error", "账号或者密码错误");
				render("login.jsp");
			}
		} else {
			setAttr("error", "验证码错误");
			render("login.jsp");
		}
	}

	public void editpwdUI() {
		render("editpwd.jsp");
	}

	/**
	 * 修改密码
	 */
	public void editpwd() {
		String oldpassword = getPara("oldpassword");
		String newpassword1 = getPara("newpassword1");
		String newpassword2 = getPara("newpassword2");
		Users user = UserUtil.getUser();
		// 当前用户不为空且用户的输入的新密码正确
		if (user != null && newpassword1.equals(newpassword2)
				&& user.getPassword().equals(CryptographyUtil.md5(oldpassword, Constants.SALT))) {
			// 加盐MD5加密
			String newpassword = CryptographyUtil.md5(newpassword1,
					Constants.SALT);
			user.setPassword(newpassword);
			if (user.update()) {
				renderText("修改成功");
			} else {
				renderText("修改失败");
			}
		} else {
			renderText("修改失败");
		}
	}

	public void listUserUI() {
		render("sys/userList.jsp");
	}

	/**
	 * 查询全部用户及其用户角色
	 */
	public void listUser() {
		String searchField = getPara("searchField");
		String searchString = getPara("searchString");
		DataGrid<UserDto> dataGrid = Users.dao.findByPage(getParaToInt("page"), getParaToInt("rows"), searchField,
				searchString);
		renderJson(dataGrid);
	}

	public void operateUser() {
		Integer id = getParaToInt("id");
		// 删除操作
		if (id != null && StringUtils.equals("del", getPara("oper"))) {
			Users u = Users.dao.findById(id);
			u.setStatus(0); // 设置为注销状态
			u.update();
			renderJson("操作成功");
		} else if (id != null && StringUtils.equals("edit", getPara("oper"))) {// 更新操作
			Users u = Users.dao.findById(id);
			u.setUsername(getPara("username"));
			u.setStatus(getParaToInt("status"));
			UserRoles userRoles = UserRoles.dao.findByUserId(id);
			userRoles.setRoleId(getParaToInt("description"));
			userRoles.update();
			// 更新密码
			if (!u.getPassword().equals(getPara("password"))) {// 已经修改
				String md5password = CryptographyUtil.md5(getPara("password"),
						Constants.SALT);
				u.setPassword(md5password);
			}
			u.update();
			renderJson("操作成功");
		} else {
			renderJson("操作失败");
		}
	}
	
	public void editUserInfoUI(){
		Users user = UserUtil.getUser();
		render("");
	}
}
